package com.raedev.forms.items

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.util.Log
import android.view.View
import com.raedev.forms.FormType
import com.raedev.forms.R
import com.raedev.forms.dialog.DatePickDialogFragment
import com.raedev.forms.listener.FormViewHolder
import com.raedev.forms.view.FormEditText
import java.text.SimpleDateFormat
import java.util.*
import java.util.logging.Logger

/**
 * 日期选择表单
 * @author RAE
 * @date 2022/09/14
 * @copyright Copyright (c) https://github.com/raedev All rights reserved.
 */
@Suppress("MemberVisibilityCanBePrivate")
@SuppressLint("SimpleDateFormat")
class ButtonFormItem(
    label: String, name: String, value: String?,
    required: Boolean = false,
    dateFormat: String? = null
) : FormItem(label, name, value) {

    companion object {
        const val STYLE_ANDROID: Int = 0
        const val STYLE_SHELL: Int = 1
    }

    override val layoutId: Int = R.layout.form_item_button
    override val formType: Int = FormType.Button.ordinal

    private var format: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd")

    override var hint: String? = null
        get() = if (field.isNullOrEmpty()) "请选择" else field

    override fun getTitleLayoutWidth(holder: FormViewHolder): Int {
        return holder.findTitleLayout().measuredWidth
    }

    override fun onLayoutChildren(holder: FormViewHolder) {
        holder.updateTitleLayout()
    }

    // 新增回调接口
    interface OnAddListener {
        fun onAdd()
    }

    var addListener: OnAddListener? = null


    init {

        this.required = required
    }

    override fun onBindViewEvent(holder: FormViewHolder) {
        super.onBindViewEvent(holder)
        holder.itemView.setOnClickListener(this::onItemClick)
    }

    override fun onUnBindViewEvent(holder: FormViewHolder) {
        holder.itemView.setOnClickListener(null)
    }

    override fun onBindViewHolder(holder: FormViewHolder) {
//        holder.setHint(R.id.tv_date, this.hint, this.viewonly)
//        holder.setText(R.id.tv_date, this.value)
    }


    /**
     * 点击事件处理
     */
    private fun onItemClick(view: View) {
        showDatePick()
    }

    /**
     * 显示Android的日期选择对话框
     */
    private fun showDatePick() {
        addListener?.onAdd()
    }


}